$(function () {
    $('#_js_loginBtn').click(function () {
        var val = $('#inputPassword').val();

        //js 正则表达语法:   /^1   $/


       // if (/^1\d{10}$/g.test(val)) {
        if (/^1[3|4|5|8][0-9]\d{4,8}$/.test(val)) {

            $.get("/checkPhone", {phone:val}, function (data) {
                if(data){
                    $('#inputPassword').next().text('').hide()
                    $('.login-box').hide()
                    $('.signup-box').show()
                    $("#phone").val(val);
                }else{
                    $('#inputPassword').next().text('手机号码已注册.').show()
                }
            })
        } else {
            $('#inputPassword').next().text('手机号码格式不正确').show()
        }
    });

    //短信发送
    $('.vcode-send').click(function () {
        if ($(this).hasClass('disabled')) {
        } else {
            var self = $(this);
            var count = 10;
            self.addClass('disabled')
            self.text(count + '秒后重新获取')
            var timer = setInterval(function () {
                count--;
                if (count > 0) {
                    self.text(count + '秒后重新获取');
                } else {
                    clearInterval(timer)
                    self.text('重新获取验证码')
                    self.removeClass("disabled");
                }

            }, 1000);

            var phone = $("#phone").val();
            $.get("/sendVerifyCode", {phone:phone}, function (data) {

                console.log(data);
                if(data.success){
                    popup("发送成功")
                }else{
                    popup(data.msg);
                }
            })
        }
    });

    //注册
    $("#editForm").ajaxForm(function (data) {
        if(data.success){
            location.href = "/login.html";
        }else{
            popup(data.msg);
        }
    })



    $("#editForm").validate({
        //验证逻辑
        rules: {
            //单个验证
            //name:"required"
            //多个验证
            phone: {
                required: true,
                minlength: 11
            },
            nickname:{
                required:true,
            },
            password: {
                required: true,
                rangelength: [1, 6]
            },
            rpassword: {
                equalTo: "#password"
            },
            verifyCode: {
                required: true
            }
        },
        //验证失败后提示
        messages: {
            //可以不写，使用默认的
            name: {
                required: "手机号必填",
                minlength: "手机号长度为11"
            },
            nickname:{
                required:"昵称不能为空"
            },
            password: {
                required:"密码不能为空",
                rangelength:"密码长度1-6位"
            },
            rpassword: {
                equalTo: "2次密码不一致"
            },
            verifyCode:{
                required:"验证码不能为空"
            }
        }
    })
});